﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TCC.ControleFluxoDeCaixa.Model.Entities;

namespace TCC.ControleFluxoDeCaixa.Model.Gerenciadores
{
    public class GerenciadorDeDatas
    {
        private static GerenciadorDeDatas _singletonInsance;
        private FluxoDeCaixaContextFiltrado _contexto;

        public void Refresh()
        {
            if (_contexto != null) _contexto.Dispose();
            _contexto = FluxoContextFactory.GetInstance().Create();
        }

        public DateTime DiaUtil(DateTime tempData)
        {
            if (_contexto == null) Refresh();
            Debug.Assert(_contexto != null, "_contexto != null");
            while (true)
            {
                if (tempData.DayOfWeek == DayOfWeek.Saturday)
                    tempData = tempData.AddDays(2);
                else if (tempData.DayOfWeek == DayOfWeek.Sunday)
                    tempData = tempData.AddDays(1);
                else
                {                    
                    if (_contexto.FeriadosDataFixa.Any(c => c.Mes == tempData.Month && c.Dia == tempData.Day))
                        tempData = tempData.AddDays(1);
                    else if (_contexto.FeriadosDataVariavel.Any(c => c.Data == tempData))
                        tempData = tempData.AddDays(1);
                    else
                        return tempData;
                }
            }
        }

        public static GerenciadorDeDatas GetInstance()
        {
            return _singletonInsance ?? (_singletonInsance = new GerenciadorDeDatas());
        }
    }
}
